package com.dt.dubai.deft.service.innate;

import com.dt.dubai.deft.entity.UUserRoleEntity;
import com.dt.dubai.deft.mapper.UUserRoleMapper;

import com.dt.dubai.core.comm.*;
import com.dt.dubai.core.exception.DtException;
import com.dt.dubai.core.exception.PageErrorMesg;
import com.dt.dubai.core.utile.I18nUtile;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

public abstract class UUserRoleSerInnateAbsImp implements UUserRoleSerInnateAbs {

    @Autowired
    protected I18nUtile i18nUtile;

    @Autowired
    protected UUserRoleMapper uUserRoleMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean savEntity(UUserRoleEntity inEntity, final Long userId) {
        Tools.createEntityDate(inEntity);
        userSavEntity(inEntity);
        ValidatorUtils.validEntity(inEntity);

        int result = uUserRoleMapper.insert(inEntity);
        if (result != ConstantValue.INT_1)
            throw new RuntimeException("E79");
        return ConstantValue.TRUE;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean modEntity(UUserRoleEntity inEntity, final Long userId) {
        UUserRoleEntity upEntity = byKeyRecordId(inEntity.getRecordId(), userId);
        if (null == upEntity) {
            throw new RuntimeException("E47");
        }
        Date oldLastupTxStamp = new Date(upEntity.getLastupTxStamp().getTime());
        Tools.updateEntityDate(upEntity);
        Example example = new Example(UUserRoleEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(ConstantValue.recordId, upEntity.getRecordId());
        criteria.andEqualTo(ConstantValue.DelSign, ConstantValue.FALSE);
        criteria.andEqualTo(ConstantValue.lastupTxStamp, oldLastupTxStamp);
        userModEntity(inEntity, upEntity);
        ValidatorUtils.validEntity(upEntity);
        int result = uUserRoleMapper.updateByExample(upEntity, example);
        if (result == ConstantValue.INT_0)
            throw new RuntimeException("E49");
        if (result != ConstantValue.INT_1)
            throw new RuntimeException("E80");
        return ConstantValue.TRUE;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean delByKey(List<Long> recordIds, final Long userId) {
        if (null == recordIds) return ConstantValue.FALSE;
        Example example = new Example(UUserRoleEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn(ConstantValue.recordId, recordIds);
        int result = uUserRoleMapper.deleteByExample(example);
        if (result == ConstantValue.INT_0)
            throw new RuntimeException("E49");
        if (result != recordIds.size())
            throw new RuntimeException("E81");
        return ConstantValue.TRUE;
    }

    @Override
    public Map<String, Object> pageByKey(Long recordId, final Long userId) {
        if (null == recordId) return null;
        Example example = new Example(UUserRoleEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(ConstantValue.recordId, recordId);
        criteria.andEqualTo(ConstantValue.DelSign, ConstantValue.FALSE);
        UUserRoleEntity entity = uUserRoleMapper.selectOneByExample(example);
        Map<String, Object> resultMap = new HashMap<>();
        if (null != entity)
            Tools.convertEntityToMap(resultMap, entity);
        return resultMap;
    }

    @Override
    public PageListData<Map<String, Object>> pageList(PageParameterData queryCondition, List<PageCondition> joinCondition, final Long userId) {
        Querys<UUserRoleEntity> querys = new Querys<>();
        Example example = new Example(UUserRoleEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(ConstantValue.DelSign, ConstantValue.FALSE);
        example.setOrderByClause(ConstantValue.OrderBylastupTxStamp);
        PageListData<UUserRoleEntity> dataList = querys.queryPageListData(uUserRoleMapper, queryCondition, joinCondition, example, criteria);
        List<Map<String, Object>> resultMap = new ArrayList<>();
        Tools.convertListEntityToMap(resultMap, dataList.getList());

        PageListData<Map<String, Object>> resultPageList = new PageListData<>(queryCondition.getPNum(), queryCondition.getPSize());
        resultPageList.setList(resultMap);
        resultPageList.setTotal(dataList.getTotal());
        return resultPageList;
    }

    @Override
    public UUserRoleEntity byKeyRecordId(Long recordId, final Long userId) {
        if (null == recordId) return null;
        Example example = new Example(UUserRoleEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(ConstantValue.recordId, recordId);
        criteria.andEqualTo(ConstantValue.DelSign,ConstantValue.FALSE);
        UUserRoleEntity entity = uUserRoleMapper.selectOneByExample(example);
        return entity;
    }

    @Override
    public List<UUserRoleEntity> listEntity(Map<String, Object> map, int limit, final Long userId) {
        Example example = new Example(UUserRoleEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(ConstantValue.DelSign, ConstantValue.INT_0);
        Tools.andEqualToCriteria(criteria, map, userId);
        example.setOrderByClause(ConstantValue.OrderBylastupTxStamp);
        if (limit <= ConstantValue.INT_0) limit = ConstantValue.INT_100;
        return uUserRoleMapper.selectByExampleAndRowBounds(example, new RowBounds(ConstantValue.INT_0, limit));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean listInsertEntity(List<UUserRoleEntity> list) {

        if (null == list || list.size() == 0) return false;
        for (UUserRoleEntity entity : list) {
            entity.setDelSign(false);
            Tools.createEntityDate(entity);
            ValidatorUtils.validEntity(entity);
        }
        int result = uUserRoleMapper.listInsertEntity(list);
        if (result != list.size()) {
            throw new RuntimeException("E76");
        }
        return ConstantValue.TRUE;
    }

    protected abstract void userSavEntity(UUserRoleEntity inEntity);
    protected abstract void userModEntity(UUserRoleEntity inEntity, UUserRoleEntity upEntity);

}
